package com.apex.lottery.controller;

import com.apex.lottery.common.errorcode.ControllerErrorCodeConstants;
import com.apex.lottery.common.exception.ControllerException;
import com.apex.lottery.common.result.CommonResult;
import com.apex.lottery.common.utils.JacksonUtil;
import com.apex.lottery.controller.param.ShortMessageLoginParam;
import com.apex.lottery.controller.param.UserPasswordLoginParam;
import com.apex.lottery.controller.param.UserRegisterParam;
import com.apex.lottery.controller.result.BaseUserInfoResult;
import com.apex.lottery.controller.result.UserLoginResult;
import com.apex.lottery.controller.result.UserRegisterResult;
import com.apex.lottery.service.UserService;
import com.apex.lottery.service.VerificationCodeService;
import com.apex.lottery.service.dto.UserDTO;
import com.apex.lottery.service.dto.UserLoginDTO;
import com.apex.lottery.service.dto.UserServiceDTO;
import com.apex.lottery.service.enums.UserIdentityEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

import static com.apex.lottery.controller.converter.UserConverter.*;

/**
 * @ClassName UserController
 * @Description 用户结果
 * @Author ZJX
 * @Date 2024/9/23 15:16
 * @Version 1.0
 */

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private VerificationCodeService verificationCodeService;

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @RequestMapping("/register")
    public CommonResult<UserRegisterResult> userRegister(@Validated @RequestBody UserRegisterParam param){
        logger.info("userRegister UserRegisterParam {}", param);
//        调用 service 服务层进行访问
        UserServiceDTO userServiceDTO = userService.register(param);

        return CommonResult.success(convertToUserRegisterResult(userServiceDTO));
    }

    /**
     * 发送验证码
     *
     * @param phoneNumber
     * @return
     */
    @RequestMapping("/verification-code/send")
    public CommonResult<Boolean> sendVerificationCode(String phoneNumber) {
        logger.info("sendVerificationCode phoneNumber:{}", phoneNumber);
        verificationCodeService.sendVerificationCode(phoneNumber);
        return CommonResult.success(Boolean.TRUE);
    }

    /**
     * 密码登录
     *
     * @param param
     * @return
     */
    @RequestMapping("/password/login")
    public CommonResult<UserLoginResult> userPasswordLogin(@Validated @RequestBody UserPasswordLoginParam param) {
        logger.info("userPasswordLogin UserPasswordLoginParam:{}", JacksonUtil.writeValueAsString(param));
        UserLoginDTO userLoginDTO = userService.login(param);

        return CommonResult.success(convertToUserLoginResult(userLoginDTO));

    }

    /**
     * 短信验证码登录
     *
     * @param param
     * @return
     */
    @RequestMapping("/message/login")
    public CommonResult<UserLoginResult> shortMessageLogin(@Validated @RequestBody ShortMessageLoginParam param) {
        logger.info("shortMessageLogin ShortMessageLoginParam:{}", JacksonUtil.writeValueAsString(param));

        UserLoginDTO userLoginDTO = userService.login(param);
        return CommonResult.success(convertToUserLoginResult(userLoginDTO));

    }

    @GetMapping("/base-user/find-list")
    public CommonResult<List<BaseUserInfoResult>> findBaseUserInfo(String identity) {
        logger.info("findBaseUserInfo identity:{}", identity);

        List<UserDTO> userDTOList = userService.findUserInfo(UserIdentityEnum.forName(identity));
        return CommonResult.success(convertToUserBaseInfoResult(userDTOList));
    }

}
